package models

import (
	"gorm.io/gorm"
)

type Order struct {
	gorm.Model
	UserID        uint        `gorm:"not null"`
	User          User        `gorm:"foreignKey:UserID"`
	TotalAmount   float64     `gorm:"not null"`
	Status        string      `gorm:"default:'pending';type:varchar(50)"`
	OrderType     string      `gorm:"not null;type:varchar(20)"` // 堂食/外卖
	Address       string      `gorm:"type:varchar(255)"`
	Phone         string      `gorm:"type:varchar(20)"`
	Items         []OrderItem `gorm:"foreignKey:OrderID"` // 明确声明关联关系
	PaymentStatus string      `gorm:"default:'unpaid';type:varchar(50)"`
	PaymentMethod string      `gorm:"type:varchar(50)"`
	IsReviewed    bool        `gorm:"-"` // 临时字段，不存储到数据库
}

type OrderItem struct {
	gorm.Model
	OrderID  uint    `gorm:"not null;index:idx_order_dish,unique"`
	Order    Order   `gorm:"foreignKey:OrderID"`
	DishID   uint    `gorm:"not null;index:idx_order_dish,unique"`
	Dish     Dish    `gorm:"foreignKey:DishID"`
	Review   Review  `gorm:"foreignKey:OrderID,DishID;references:OrderID,DishID"`
	Quantity int     `gorm:"not null"`
	Price    float64 `gorm:"not null"`
	Subtotal float64 `gorm:"not null"`
}
